package main

import (
	"demo/reflect"
	"fmt"
	r "reflect"
)

func main() {
	//reflect.TestType1()
	//reflect.TestStructField()
	//Test1()
	TestFromMap()
	//TestElem()
	//TestSlice()
}

func Test1() {
	var data = make(map[string]interface{})
	data["int"] = 8
	data["int8"] = int8(8)
	data["int16"] = int16(8)
	data["int32"] = int32(8)
	data["int64"] = int64(8)

	data["uint"] = uint(8)
	data["uint8"] = uint8(8)
	data["uint16"] = uint16(8)
	data["uint32"] = uint32(8)
	data["uint64"] = uint64(8)

	data["float32"] = float32(2.2)
	data["float64"] = 2.2
	data["complex64"] = complex64(1 + 2i)
	data["complex128"] = 1 + 2i

	data["string"] = "uydfhsudgfr"
	data["string2"] = "aaaaaa林芳伊"

	data["array1"] = [3]int{1, 2, 3}

	type t struct {
		a int
		b int
	}
	data["array2"] = [2]t{t{1, 2}, t{3, 4}}

	data["struct"] = t{1, 3}
	data["slice1"] = []t{}
	data["slice2"] = []t{t{1, 2}, t{3, 4}, t{5, 6}}

	data["map1"] = map[string]interface{}{"1": 1, "2": "a"}
	data["map2"] = map[string]interface{}{"name": "林芳伊", "age": 28}

	for k, v := range data {
		size := reflect.GetDataSize(&v)
		fmt.Printf("key=%s, v=%d\n", k, size)
	}

	size := int(r.TypeOf(data["map1"]).Size()) //map内部结构类型  大小8
	fmt.Println(size)
	size = int(r.TypeOf(data["slice2"]).Size()) //slice 内部结构类型  大小24
	fmt.Println(size)
	size = int(r.TypeOf(data["struct"]).Size()) //struct值大小
	fmt.Println(size)

	size = int(r.TypeOf("aaaaadgdfsgf").Size()) //string内部结构类型 大小16
	fmt.Println(size)

	f := func() {
		fmt.Println("hello")
	}
	size = int(r.TypeOf(f).Size()) //function指针大小为8
	fmt.Println(size)

	var ch = make(chan int8, 1)
	size = int(r.TypeOf(ch).Size()) //channel指针大小为8
	fmt.Println(size)
}

func TestFromMap() {
	type Sub struct {
		A int
		B int
	}

	type Person struct {
		Name string
		Age  int
	}

	type A struct {
		A   int
		Age int
	}

	type Test struct {
		I     int
		F     float64
		C     complex128
		S     string
		ARR   [3]int
		SLICE []int
		SUB   Sub
		M     A
		M1    Person
	}

	m := make(map[string]interface{})
	m["i"] = 1
	m["f"] = 2.3
	m["c"] = 2.0 + 3i
	m["s"] = "aaa"
	m["arr"] = [3]int{1, 2, 3}
	m["slice"] = []int{1, 2, 3, 45, 7}
	m["sub"] = map[string]interface{}{
		"A": 11,
		"B": 22,
	}
	m["m"] = map[string]interface{}{
		"A":   1,
		"Age": 18,
	}
	m["m1"] = Person{"linfangyi", 18}

	var p = reflect.FromMap(m, &Test{})
	fmt.Println(p)

	m1, err := reflect.ToMap(p)
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println(m1)
}

func TestElem() {
	i := 1
	b := [2]int{1, 2}
	s := []string{"a", "b", "c"}

	fmt.Println(r.ValueOf(i).Kind()) //int
	fmt.Println(r.ValueOf(b).Kind()) //array
	fmt.Println(r.ValueOf(s).Kind()) //slice

	//指针元素
	fmt.Println(r.ValueOf(&i).Elem().Kind()) //int
	fmt.Println(r.ValueOf(&b).Elem().Kind()) //array
	fmt.Println(r.ValueOf(&s).Elem().Kind()) //slice
	var in interface{} = &i
	fmt.Println(r.ValueOf(in).Elem().Kind()) //int

	fmt.Println(r.ValueOf(&i))
	fmt.Println(r.ValueOf(&b))
	fmt.Println(r.ValueOf(&s))
	fmt.Println(r.ValueOf(&i).Elem()) //指针指向的元素
	fmt.Println(r.ValueOf(&b).Elem()) //数组元素
	fmt.Println(r.ValueOf(&s).Elem()) //切片元素

	m := map[string]interface{}{
		"name": "",
		"age":  nil,
	}

	value := r.ValueOf(m).MapIndex(r.ValueOf("name"))
	fmt.Println(value, value.IsValid())

	value = r.ValueOf(m).MapIndex(r.ValueOf("age"))
	fmt.Println(value, value.IsValid())

	value = r.ValueOf(m).MapIndex(r.ValueOf("a"))
	fmt.Println(value, value.IsValid())

	v, ok := m["name"]
	fmt.Println(v, ok)
	v, ok = m["name1"]
	fmt.Println(v, ok)
}

func TestSlice() {
	var s = make([]int, 3, 3)
	s[0] = 1
	s[1] = 11
	s[2] = 111
	fmt.Println(s)
}
